import frappe

def custom_send_welcome_mail_to_user(doc, method=None):
    # 禁止系统自动发
    doc.flags.disable_welcome_email = True
    doc.flags.no_welcome_mail = True  # 兼容不同版本

    # 判断下用户是不是新用户，已激活等等
    if not doc.enabled:
        return
    # 确保reset_password_key已生成
    if not doc.reset_password_key:
        doc.reset_password_key = frappe.generate_hash(length=32)
        doc.save(ignore_permissions=True)

    template = "new_user"
    full_name = doc.full_name or doc.first_name
    link = f"{frappe.conf.site_url}update-password?key={doc.reset_password_key}"
    login_url = f"{frappe.conf.site_url}#login"
    frappe.sendmail(
        recipients=[doc.email],
        subject="欢迎加入我们！",
        template=template,
        args={
            "full_name": full_name,
            "email": doc.email,
            "activate_link": link,
            "login_url": login_url,
        },
        # header=["账户激活", "green"],
    )
